package cn.itcast.scala.course09

import java.sql.{Connection, DriverManager}

/**
  * scala操作数据库MySQL
  * create by zhangtian on 2019/1/10
  */
object MySQLApp {
  def main(args: Array[String]): Unit = {
    val driver = "com.mysql.jdbc.Driver"
    val url = "jdbc:mysql://192.168.177.207:3306/mini_jbeanscoin"
    val username = "jbeanscoin_user"
    val password = "jbeanscoin_user"

    var connection :Connection = null

    try {
      classOf[com.mysql.cj.jdbc.Driver]
      connection = DriverManager.getConnection(url, username, password)
      val statement = connection.createStatement()
      val resultSet = statement.executeQuery("select activity_share_title as title,activity_share_content as content from mini_activity_share")
      while (resultSet.next()) {
        val title = resultSet.getNString("title")
        val content = resultSet.getNString("content")

        println(s"$title, $content")
      }
    }catch {
      case e:Exception => e.printStackTrace()
    } finally {
      if (connection != null){
        connection.close()
      }
    }
  }
}
